package com.jk.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.jk.bean.*;
import com.jk.mapper.UserMapper;
import com.jk.service.UserService;
import com.jk.utils.ReceivePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {


    @Resource
    private UserMapper userMapper;


    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public SendPage queryWeekSentiment(ReceivePage rp) {


        List<Author> list = userMapper.queryWeekSentiment();

        PageHelper.startPage(rp.getPage(),rp.getRows());
        List<Author> data = userMapper.queryWeekSentiment();

        return new SendPage(list.size(),data);
    }

    @Override
    public void addASentiment(String id) {
        userMapper.addASentiment(id);
    }

    @Override
    public void addArticleAll(String id, String tableName) {
        userMapper.addArticleAll(id,tableName);
    }


    @Override
    public List<Content> querySpace(String id) {
        Query query = new Query();


        if(!StringUtils.isEmpty(id)) {
            query.addCriteria(Criteria.where("authorid").is(id));
        }
        List<Content> cn= mongoTemplate.find(query, Content.class);
        return  cn;
    }

    @Override
    public Author queryAuthor(String id) {
        return userMapper.queryAuthor(id);
    }

    @Override
    public User tologin(User user) {
        return userMapper.tologin(user);
    }

    @Override
    public List<Author> querynoAuthor(String id) {
        List<Author> list = new ArrayList<>();
        List<Author> authors = userMapper.querynoAuthor(id);

        for (Author author : authors) {
            list.add(author);
            if(list.size()==5){
                break;
            }
        }
        return list;
    }


    @Override
    public Finance querybkcount(Finance fn) {
        Finance querybkcount = null;
        if(fn!=null){
          querybkcount = userMapper.querybkcount(fn);
        }

        return querybkcount;
    }

    @Override
    public List<Author> queryWeekSentiment2(ReceivePage rp) {
        return null;
    }
    //+积分
    @Override
    public void updatejifen(Integer price1, Integer userId) {
        userMapper.updatejifen(price1,userId);
    }
    //如果vip等级相等 延迟相应月份
    @Override
    public void updateVipJia(int month, Integer userId) {
        userMapper.updateVipJia(month,userId);
    }
    //如果vip等级不相等 当前时间加相应月份
    @Override
    public void updateVipHuan(int leixing, int month, Integer userId) {
        userMapper.updateVipHuan(leixing,month,userId);
    }

    @Override
    public User queryuserbybzname(Integer userId) {

        User user=userMapper.queryuserbybzname(userId);
        return user;
    }

    @Override
    public void chongzhi(Chongzhijilu chongZhi2) {
        userMapper.chongzhijilu(chongZhi2);
    }

    @Override
    public void jifenchongzhi(Chongzhijilu chongZhi2) {
        userMapper.jifenchongzhi(chongZhi2);
    }

    @Override
    public User queryUserbyid(String id) {
        User user=userMapper.queryUserbyid(id);
        return user;
    }

    @Override
    public void addQQ(QQ qq) {

        User user = userMapper.queryUserByIdQQ(qq);

        if(user==null){

        if(qq.getGender().equals("男")){
            qq.setSex(1);
        }else {
            qq.setSex(2);
        }
         userMapper.addQQ(qq);
        }
    }

    //定时删除  清空人气量
    public static void deleteSentiment() {

        UserMapper.deleteSentiment();
    }
}
